/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.core.launchers;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.debug.ui.launchConfigurations.JavaMainTab;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipsejcde.core.JCDECore;

/**
 * @author Amgad S Youssef
 *
 */
public class JCToolLauncher extends AbstractLauncher {
	public static final int TOOL_TYPE_CONVERTER = 1;

	public static final int TOOL_TYPE_CAPGEN = 2;

	public static final int TOOL_TYPE_SCRIPTGEN = 3;

	public static final int TOOL_TYPE_APDUTOOL = 4;

	public static final int TOOL_TYPE_VERIFYCAP = 5;

	public static final int TOOL_TYPE_VERIFYEXP = 6;

	private int toolType;

	private JCToolLauncher(int toolType) {
		this.toolType = toolType;
	}

	public static ILauncher getLauncher(int toolType) {
		if (mapToolType(toolType) == null) {
			return null;
		}
		ILauncher launcher = new JCToolLauncher(toolType);
		return launcher;
	}

	private static String mapToolType(int toolType) {
		switch (toolType) {
		case TOOL_TYPE_CONVERTER:
			return "com.sun.javacard.converter.Converter";
		case TOOL_TYPE_CAPGEN:
			return "com.sun.javacard.jcasm.cap.Main";
		case TOOL_TYPE_SCRIPTGEN:
			return "com.sun.javacard.scriptgen.Main";
		case TOOL_TYPE_APDUTOOL:
			return "com.sun.javacard.apdutool.Main";
		case TOOL_TYPE_VERIFYCAP:
			return "com.sun.javacard.offcardverifier.Verifier";
		case TOOL_TYPE_VERIFYEXP:
			return "com.sun.javacard.offcardverifier.VerifyExp";
		}
		return null;
	}

	@Override
	protected Map<String, Serializable> getLaunchConfigurationAttributes(String arguments) throws Exception {
		Map<String, Serializable> attributes = new HashMap<String, Serializable>();
		attributes.put("org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND", new Boolean(true));
		attributes.put("org.eclipse.debug.core.capture_output", new Boolean(true));
		attributes.put("org.eclipse.debug.ui.ATTR_CONSOLE_OUTPUT_ON", new Boolean(true));

		attributes.put(IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH, new Boolean(false));
		attributes.put(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, mapToolType(this.toolType));
		attributes.put(JavaMainTab.ATTR_INCLUDE_EXTERNAL_JARS, new Boolean(true));
		ArrayList<String> classPathList = new ArrayList<String>();
		File libDir = new File(JCDECore.getCurrentJCDKHome() + File.separator + "lib");
		File[] jcjars = libDir.listFiles();
		for (int i = 0; i < jcjars.length; i++) {
			if (jcjars[i].getName().endsWith(".jar")) {
				IPath path = new Path(jcjars[i].getAbsolutePath());
				IRuntimeClasspathEntry runtimeClasspathEntry = JavaRuntime.newArchiveRuntimeClasspathEntry(path);
				classPathList.add(runtimeClasspathEntry.getMemento());
			}
		}
		attributes.put(IJavaLaunchConfigurationConstants.ATTR_CLASSPATH, classPathList);
		attributes.put(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, arguments);
		return attributes;
	}

	@Override
	protected String getLaunchConfigurationTypeID() {
		return IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION;
	}

	@Override
	protected String getLauncherName() {
		return "Java Card Tool";
	}
}
